package apiexample.transform.statistics;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import com.apiexample.server.dao.BaseDao;
import com.apiexample.server.dao.ClassDao;
import com.apiexample.server.dao.ClusterDao;
import com.apiexample.server.dao.ExampleClassDao;
import com.apiexample.server.dao.ExampleDao;
import com.apiexample.server.entity.ClassEntity;
import com.apiexample.server.entity.ClusterEntity;
import com.apiexample.server.entity.ExampleClassRelation;
import com.apiexample.server.entity.ExampleEntity;
import com.apiexample.server.util.StringDealer;

import dao.PageDao;
import dao.entity.PageEntity;

public class StatisticsOnServerDB {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		int projectId = 11;
//		examplesPerPage(projectId);	
//		examplesPerAPI(projectId);
//		clustersPerAPI(projectId);
		totalEmpPages();
	}
	
	public static void examplesPerPage(int projectId) throws FileNotFoundException{
		// TODO Auto-generated method stub

		PrintStream fos_out = new PrintStream(new File("M:/apiexample/statistic_project_emp_per_page_"+projectId+".txt"));
		System.setOut(fos_out);
		System.out.println("Statistic on examples per page for project"+projectId);
		
		Connection server_conn = BaseDao.getTestConn();
		 Connection example_conn = dao.BaseDao.getTestConn();
		 
//		 CentralDataPool.persist();
		 List<ClassEntity> classes = ClassDao.getAllClassList(server_conn);
		 HashSet<Integer> classIdsInProject = new HashSet<Integer>();
		 for (ClassEntity classEntity : classes) {
			if(classEntity.getProject_id()==projectId){
				classIdsInProject.add(classEntity.getId());
			}
		}
		 ArrayList<String> pageLinkList = PageDao.getLinkList(example_conn, PageEntity.EXTRACT_CODE_OK);
		 
		
		 
		 HashMap<String, Integer> linkMapExamples = new HashMap<String, Integer>();
		 
		 HashMap<String, Integer> linkMapClass = new HashMap<String, Integer>();
		 
		 for(Integer classinproject:classIdsInProject){
			 ArrayList<ExampleEntity> exampleList = ExampleDao.getExampleListByClassID(server_conn,classinproject.intValue());
			 for (ExampleEntity exampleEntity : exampleList) {
				String link = exampleEntity.getSource();
				Integer classId = linkMapClass.get(link);
				if(classId==null||classId.intValue()==exampleEntity.getClass_id()){
					if(classId == null){
						linkMapClass.put(link, exampleEntity.getClass_id());
					}
					Integer exampleNums = linkMapExamples.get(link);
					if(exampleNums==null){
						linkMapExamples.put(link, 1);
					}else{
						int new_num = exampleNums.intValue()+1;
						linkMapExamples.remove(link);
						linkMapExamples.put(link, new_num);
					}
				}			
			}
		 }
		 
		 System.out.println("PAGELINK\tEXAMPLES");
		 for(String pageLink : pageLinkList){
			 Integer examples = linkMapExamples.get(pageLink);
			 if(examples==null){
				 System.out.println(pageLink+"\t"+0);
			 }else{
				 System.out.println(pageLink+"\t"+examples.intValue());
			 }
		 }
		 fos_out.close();
		try {
			server_conn.close();
			example_conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void examplesPerAPI(int projectId) throws FileNotFoundException{
		PrintStream fos_out = new PrintStream(new File("M:/apiexample/statistic_project_emp_per_api_"+projectId+".txt"));
		System.setOut(fos_out);
		System.out.println("Statistic on examples per api for project"+projectId);
		
		Connection server_conn = BaseDao.getTestConn();
		 Connection example_conn = dao.BaseDao.getTestConn();
		 
//		 CentralDataPool.persist();
		 List<ClassEntity> classes = ClassDao.getAllClassList(server_conn);
		 HashSet<Integer> classIdsInProject = new HashSet<Integer>();
		 HashMap<String, Integer> classNameMapExamples = new HashMap<String, Integer>();
		 for (ClassEntity classEntity : classes) {
			if(classEntity.getProject_id()==projectId){
				ArrayList<Integer> exampleIds = ExampleClassDao.getExampleIDListByClassID(server_conn, classEntity.getId(), ExampleClassRelation.REL_TYPE_DIRECT_GET);
				if(exampleIds!=null){
					classNameMapExamples.put(classEntity.getName(), exampleIds.size());
				}else{
					classNameMapExamples.put(classEntity.getName(), 0);
				}
//				classIdsInProject.add(classEntity.getId());
			}
		}
		 System.out.println("Examples per API:");
		 System.out.println("======================================");
		 System.out.println("APINAME\tEXAMPLES");
		 Iterator<String> classNames = classNameMapExamples.keySet().iterator();
		 while(classNames.hasNext()){
			 String className = classNames.next();
			 System.out.println(className+"\t"+classNameMapExamples.get(className));
		 }
		 
		 fos_out.close();
			try {
				server_conn.close();
				example_conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public static void clustersPerAPI(int projectId) throws FileNotFoundException{
		PrintStream fos_out = new PrintStream(new File("M:/apiexample/statistic_project_cluster_per_api_"+projectId+".txt"));
		System.setOut(fos_out);
		System.out.println("Statistic on clusters per api for project"+projectId);
		
		Connection server_conn = BaseDao.getTestConn();
		 Connection example_conn = dao.BaseDao.getTestConn();
		 
//		 CentralDataPool.persist();
		 List<ClassEntity> classes = ClassDao.getAllClassList(server_conn);
		 HashSet<Integer> classIdsInProject = new HashSet<Integer>();
		 HashMap<String, Integer> classNameMapClusters = new HashMap<String, Integer>();
		 for (ClassEntity classEntity : classes) {
			if(classEntity.getProject_id()==projectId){
				classIdsInProject.add(classEntity.getId());
				ArrayList<ClusterEntity> clusterList = ClusterDao.getClusterListByClassID(server_conn, classEntity.getId());
//				classIdMapName.put(classEntity.getId(), classEntity.getName());
				if(clusterList==null){
					classNameMapClusters.put(classEntity.getName(), 0);
				}else{
					classNameMapClusters.put(classEntity.getName(), clusterList.size());
				}
			}
		}
		 System.out.println("Clusters per API:");
		 System.out.println("======================================");
		 System.out.println("APINAME\tCLUSTERS");
		 Iterator<String> classNames = classNameMapClusters.keySet().iterator();
		 while(classNames.hasNext()){
			 String className = classNames.next();
			 System.out.println(className+"\t"+classNameMapClusters.get(className));
		 }
		 
		 fos_out.close();
			try {
				server_conn.close();
				example_conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public static void totalEmpPages() throws FileNotFoundException{
		PrintStream fos_out = new PrintStream(new File("M:/apiexample/statistic_empnumbers.txt"));
		System.setOut(fos_out);
		HashSet<String> exampleSet = new HashSet<String>();
		HashSet<String> linkSet = new HashSet<String>();
		Connection server_conn = BaseDao.getTestConn();
		long lineCount = 0;
		ArrayList<ExampleEntity> allExamples = ExampleDao.getAllExampleList(server_conn);
		for (ExampleEntity example : allExamples) {
			if(exampleSet.add(example.getCode_content()+example.getSource())){
				lineCount+= StringDealer.getLineNumber(example.getCode_content());
			}
			linkSet.add(example.getSource());
			
		}
		System.out.println("Number of examples:");
		System.out.println(exampleSet.size());
		System.out.println("Number of unique web pages");
		System.out.println(linkSet.size());
		System.out.println("Average LOC:");
		System.out.println(lineCount/((double)exampleSet.size()));
		 fos_out.close();
			try {
				server_conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
